package com.itwn.portal.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.itwn.portal.entity.Section;
import com.itwn.portal.mapper.SectionMapper;
import com.itwn.portal.vo.SectionReadVo;
import com.itwn.portal.vo.SectionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.*;

@Service
public class SectionService {
    @Autowired
    SectionMapper sectionMapper;

    public Map<Integer,List<SectionVo>> queryweekdaySectionList(Integer weekday) {
        //获取存储容器
        Map<Integer,List<SectionVo>> map = new HashMap<>();
        weekday+=7;
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MILLISECOND, 0);
        for (Integer i=0;i<7;i++){
            Integer w=weekday-i;
            if(i==0){
                c.add(Calendar.DATE, +1);
            }else {
                c.add(Calendar.DATE, +0);
            }
            Date endTime = c.getTime();
            c.add(Calendar.DATE, -1);
            Date starTime= c.getTime();
            List<SectionVo> list1 = sectionMapper.selectSectionByTime(starTime, endTime, 5);
            map.put(w%7,list1);
        }
        return map;
    }

    public SectionReadVo querySectionReadVo(Integer rid,Integer sid) {
        return sectionMapper.selectSectionReadVo(rid,sid);
    }

    @Cacheable(value = {"section"},keyGenerator = "myGenerateKey")
    public PageInfo<Section> querySectionByPage(Integer size, Integer now,Integer id) {
        PageMethod.startPage(now,size);
        List<Section> list = sectionMapper.selectAllById(id);
        return new PageInfo<>(list);
    }

    @Cacheable(value = {"section"},keyGenerator = "myGenerateKey")
    public List<Section> queryLatestSection(Integer id) {
        return this.sectionMapper.selectLatest(id);
    }


    public Integer queryNext(Integer rid, Integer sid) {
        return sectionMapper.selectNextSectionid(rid,sid);
    }
}
